﻿// -------------------------------------------------------------------------
//    @FileName         :    NFSnsRoleDetail.cpp
//    @Author           :    gaoyi
//    @Date             :    2025-03-26
//    @Email			:    445267987@qq.com
//    @Module           :    NFSnsRoleDetail
//
// -------------------------------------------------------------------------

#include "NFSnsRoleDetail.h"

#include <NFSnsDetailPart.h>
#include <NFTransSaveDetailDB.h>

#include "NFSnsServer/NFSnsCommon/NFSnsRoleSimpleService.h"

NFSnsRoleDetail::NFSnsRoleDetail()
{
    if (NFShmMgr::Instance()->GetCreateMode() == EN_OBJ_MODE_INIT)
    {
        CreateInit();
    }
    else
    {
        ResumeInit();
    }
}

NFSnsRoleDetail::~NFSnsRoleDetail()
{
}

int NFSnsRoleDetail::CreateInit()
{
    m_ullRoleId = 0;
    m_pPart.resize(PART_SNS_DETAIL_MAX);
    return 0;
}

int NFSnsRoleDetail::ResumeInit()
{
    return 0;
}

int NFSnsRoleDetail::Init()
{
    SetPlayerStatus(CACHE_STATUS_NONE);
    std::vector<NFSnsDetailPart*> vec;
    for (uint32_t i = 0; i < PART_SNS_DETAIL_MAX; ++i)
    {
        m_pPart[i] = CreatePart(i);
        if (nullptr == m_pPart[i])
        {
            NFLogError(NF_LOG_DEFAULT, m_ullRoleId, "Player Init, Create Part Failed, playerId:{} part:{}", m_ullRoleId, i);
            for (size_t i = 0; i < vec.size(); i++)
            {
                FindModule<NFIMemMngModule>()->DestroyObj(vec[i]);
            }
            return -1;
        }
        vec.push_back(m_pPart[i].GetPoint());
    }

    return 0;
}

int NFSnsRoleDetail::UnInit()
{
    for (uint32_t i = 0; i < PART_SNS_DETAIL_MAX; ++i)
    {
        if (m_pPart[i])
        {
            RecylePart(m_pPart[i]);
        }
    }

    return 0;
}

int NFSnsRoleDetail::LoadFromDb(const proto_ff::tbSnsRoleDetail* pDbData)
{
    for (uint32_t i = 0; i < m_pPart.size(); ++i)
    {
        if (m_pPart[i])
        {
            m_pPart[i]->LoadFromDb(pDbData);
        }
    }
    return 0;
}

int NFSnsRoleDetail::SaveDb(proto_ff::tbSnsRoleDetail* pDbData)
{
    CHECK_NULL(m_ullRoleId, pDbData);
    pDbData->set_role_id(m_ullRoleId);
    for (uint32_t i = 0; i < m_pPart.size(); ++i)
    {
        if (m_pPart[i])
        {
            m_pPart[i]->SaveDb(pDbData);
        }
    }
    return 0;
}

int NFSnsRoleDetail::Tick()
{
    for (uint32_t i = 0; i < m_pPart.size(); ++i)
    {
        if (m_pPart[i])
        {
            m_pPart[i]->Tick();
        }
    }
    SaveToDb(60, TRANS_SAVEROLEDETAIL_NORMAL, false);

    auto pRoleSimple = GetRoleSimple();
    if (pRoleSimple)
    {
        pRoleSimple->SetCacheExpireTime();
        pRoleSimple->SaveToDb(60, TRANS_SAVEROLEDETAIL_NORMAL, false);
    }
    return 0;
}

bool NFSnsRoleDetail::IsNeedSave() const
{
    if (IsUrgentNeedSave())
    {
        return true;
    }

    for (uint32_t i = 0; i < m_pPart.size(); ++i)
    {
        if (m_pPart[i] && m_pPart[i]->IsUrgentNeedSave())
        {
            return true;
        }
    }

    return false;
}

int NFSnsRoleDetail::SendTransToDb(int iReason)
{
    NFTransSaveDetailDB* pTrans = NFTransSaveDetailDB::CreateTrans();
    CHECK_NULL_WF(GetRoleId(), pTrans, "CreateTrans failed, NFTransSaveDetailDB used num:{} all num:{}", NFTransSaveDetailDB::GetStaticUsedCount(), NFTransSaveDetailDB::GetStaticItemCount());
    int iRetCode = pTrans->SaveDb(this, iReason);
    CHECK_ERR_AND_FIN_TRANS(iRetCode, pTrans, "SaveDb failed, iReason:{}", iReason);
    return 0;
}

NFSnsRoleSimple* NFSnsRoleDetail::GetRoleSimple()
{
    return NFSnsRoleSimpleService::GetRoleSimple(m_ullRoleId);
}

int NFSnsRoleDetail::SetCorpsId(uint64_t ullCorpsId)
{
    auto pRoleSimple = GetRoleSimple();
    CHECK_NULL(m_ullRoleId, pRoleSimple);
    auto& stData = pRoleSimple->GetData();
    stData.role_info.battle_corps_id = ullCorpsId;
    pRoleSimple->MarkDirty();
    return 0;
}

int NFSnsRoleDetail::SetRoleTax(int value)
{
    auto pRoleSimple = GetRoleSimple();
    CHECK_NULL(m_ullRoleId, pRoleSimple);
    auto& stData = pRoleSimple->GetData();
    stData.role_info.role_tax = value;
    pRoleSimple->MarkDirty();
    return 0;
}

int NFSnsRoleDetail::SetName(const std::string& name)
{
    auto pRoleSimple = GetRoleSimple();
    CHECK_NULL(m_ullRoleId, pRoleSimple);
    auto& stData = pRoleSimple->GetData();
    stData.role_info.name = name;
    pRoleSimple->MarkDirty();
    return 0;
}

int NFSnsRoleDetail::SetLastLogoutTime(uint64_t time)
{
    auto pRoleSimple = GetRoleSimple();
    CHECK_NULL(m_ullRoleId, pRoleSimple);
    auto& stData = pRoleSimple->GetData();
    stData.role_info.last_logout_time = time;
    pRoleSimple->MarkDirty();
    return 0;
}

int NFSnsRoleDetail::SetGuildInfo(uint64_t guildId, proto_ff::enGuildMemTitle title, const std::string& name)
{
    auto pRoleSimple = GetRoleSimple();
    CHECK_NULL(m_ullRoleId, pRoleSimple);
    auto& stData = pRoleSimple->GetData();
    stData.role_info.guild_id = guildId;
    stData.role_info.guild_title = static_cast<enGuildMemTitle>(title);
    stData.role_info.guild_name = name;
    pRoleSimple->MarkDirty();
    return 0;
}

int NFSnsRoleDetail::ClearGuildInfo()
{
    auto pRoleSimple = GetRoleSimple();
    CHECK_NULL(m_ullRoleId, pRoleSimple);
    auto& stData = pRoleSimple->GetData();
    stData.role_info.guild_id = 0;
    stData.role_info.guild_title = EN_MEM_TITLE_NONE;
    stData.role_info.guild_name.clear();
    MarkDirty();

    return 0;
}

uint32_t NFSnsRoleDetail::GetAllSeq() const
{
    int seq = GetCurSeq();
    for (uint32_t i = 0; i < m_pPart.size(); ++i)
    {
        if (m_pPart[i])
        {
            seq += m_pPart[i]->GetCurSeq();
        }
    }
    return seq;
}

void NFSnsRoleDetail::ClearAllSeq()
{
    ClearUrgent();
    for (uint32_t i = 0; i < m_pPart.size(); ++i)
    {
        if (m_pPart[i])
        {
            m_pPart[i]->ClearUrgent();
        }
    }
}

NFSnsDetailPart* NFSnsRoleDetail::GetPart(uint32_t partType)
{
    if (partType < static_cast<uint32_t>(m_pPart.size()))
    {
        return m_pPart[partType].GetPoint();
    }

    return nullptr;
}

const NFSnsDetailPart* NFSnsRoleDetail::GetPart(uint32_t partType) const
{
    if (partType < static_cast<uint32_t>(m_pPart.size()))
    {
        return m_pPart[partType].GetPoint();
    }

    return nullptr;
}

NFSnsDetailPart* NFSnsRoleDetail::CreatePart(uint32_t partType)
{
    auto pPart = dynamic_cast<NFSnsDetailPart*>(FindModule<NFIMemMngModule>()->CreateObj(EOT_SNS_DETAIL_PART_MIN_ID + partType));
    if (pPart)
    {
        int iRet = pPart->Init(this, partType);
        if (iRet != 0)
        {
            NFLogError(NF_LOG_DEFAULT, 0, "{}::Init Failed", pPart->GetClassName());
            FindModule<NFIMemMngModule>()->DestroyObj(pPart);
            return nullptr;
        }
    }
    else
    {
        NFLogError(NF_LOG_DEFAULT, m_ullRoleId, "Create Part Failed, partType Not Handle:{}", partType);
    }
    return pPart;
}

int NFSnsRoleDetail::RecylePart(NFSnsDetailPart* pPart) const
{
    CHECK_NULL(0, pPart);
    pPart->UnInit();
    FindModule<NFIMemMngModule>()->DestroyObj(pPart);
    return 0;
}

int NFSnsRoleDetail::OnRegister()
{
    for (uint32_t i = 0; i < m_pPart.size(); ++i)
    {
        if (m_pPart[i])
        {
            m_pPart[i]->OnRegister();
        }
    }
    return 0;
}

int NFSnsRoleDetail::OnLogin()
{
    for (uint32_t i = 0; i < m_pPart.size(); ++i)
    {
        if (m_pPart[i])
        {
            m_pPart[i]->OnLogin();
        }
    }
    return 0;
}

int NFSnsRoleDetail::OnLogout()
{
    for (uint32_t i = 0; i < m_pPart.size(); ++i)
    {
        if (m_pPart[i])
        {
            m_pPart[i]->OnLogout();
        }
    }
    return 0;
}

int NFSnsRoleDetail::OnDisconnect()
{
    for (uint32_t i = 0; i < m_pPart.size(); ++i)
    {
        if (m_pPart[i])
        {
            m_pPart[i]->OnDisconnect();
        }
    }
    return 0;
}

int NFSnsRoleDetail::OnReconnect()
{
    for (uint32_t i = 0; i < m_pPart.size(); ++i)
    {
        if (m_pPart[i])
        {
            m_pPart[i]->OnReconnect();
        }
    }
    return 0;
}

int NFSnsRoleDetail::DailyZeroTick()
{
    for (uint32_t i = 0; i < m_pPart.size(); ++i)
    {
        if (m_pPart[i])
        {
            m_pPart[i]->DailyZeroTick();
        }
    }
    return 0;
}

int NFSnsRoleDetail::DailyOtherTick()
{
    for (uint32_t i = 0; i < m_pPart.size(); ++i)
    {
        if (m_pPart[i])
        {
            m_pPart[i]->DailyOtherTick();
        }
    }
    return 0;
}

int NFSnsRoleDetail::WeekZeroTick()
{
    for (uint32_t i = 0; i < m_pPart.size(); ++i)
    {
        if (m_pPart[i])
        {
            m_pPart[i]->WeekZeroTick();
        }
    }
    return 0;
}

int NFSnsRoleDetail::MonthZeroTick()
{
    for (uint32_t i = 0; i < m_pPart.size(); ++i)
    {
        if (m_pPart[i])
        {
            m_pPart[i]->MonthZeroTick();
        }
    }
    return 0;
}
